package com.nulldev.util.internal.backport.httpclient_rw;

import static com.nulldev.util.internal.backport.httpclient_rw.impl.common.Utils.ALLOWED_HEADERS;

import java.net.URI;
import java.time.Duration;

import com.nulldev.util.internal.backport.concurrency9.Objects;
import com.nulldev.util.internal.backport.httpclient_rw.HttpClient.Version;
import com.nulldev.util.internal.backport.optionals.Optional;

final class ImmutableHttpRequest extends HttpRequest {

	private final String method;
	private final URI uri;
	private final HttpHeaders headers;
	private final Optional<BodyPublisher> requestPublisher;
	private final boolean expectContinue;
	private final Optional<Duration> timeout;
	private final Optional<Version> version;

	/** Creates an ImmutableHttpRequest from the given builder. */
	ImmutableHttpRequest(HttpRequestBuilderImpl builder) {
		this.method = Objects.requireNonNull(builder.method());
		this.uri = Objects.requireNonNull(builder.uri());
		this.headers = HttpHeaders.of(builder.headersBuilder().map(), ALLOWED_HEADERS);
		this.requestPublisher = Optional.ofNullable(builder.bodyPublisher());
		this.expectContinue = builder.expectContinue();
		this.timeout = Optional.ofNullable(builder.timeout());
		this.version = Objects.requireNonNull(builder.version());
	}

	@Override
	public String method() {
		return method;
	}

	@Override
	public URI uri() {
		return uri;
	}

	@Override
	public HttpHeaders headers() {
		return headers;
	}

	@Override
	public Optional<BodyPublisher> bodyPublisher() {
		return requestPublisher;
	}

	@Override
	public boolean expectContinue() {
		return expectContinue;
	}

	@Override
	public Optional<Duration> timeout() {
		return timeout;
	}

	@Override
	public Optional<Version> version() {
		return version;
	}

	@Override
	public String toString() {
		return uri.toString() + " " + method;
	}
}